package com._public._ui 
{
      import com.monkey.phone.ui.PhScrollBar;
      
      import flash.display.Graphics;
      import flash.display.MovieClip;
      import flash.display.Shape;
      import flash.display.Sprite;
      import flash.events.MouseEvent;
      import flash.text.*;

      public class DrawPieGraph extends MovieClip {
 
         //存放shape对象
        private var __contain:Object;
        //设置角度从-90开始
         private var R:int = -90;
         private var D:uint = 30;
         private var _shape:Shape;
         //初始饼图的圆心位置
         public var _x0:Number;
         public var _y0:Number;
         //椭圆饼图的长轴与短轴长度
         public var _a:Number;
         public var _b:Number;
         //饼图的厚度
         public var _h:Number;
         //透明度
         public var _alpha:Number;
         //数据列表
         public var _nameList:Array;
         public var _dataList:Array;
         public var _colorList:Array;
         public var _angleList:Array;
         public var _depthList:Array;
         public var showorder:Boolean;
         public var percent:Boolean;
         public var fontcolor:uint;
         public var orderx:Number;
         public var ordery:Number;
         public var orderxy:Number;
         private var shape:Array;
         private var sp:Sprite;
         private var Rect:Sprite;
         public var namelabel:TextField;
		 private var bTGOut:Boolean = false;
 
         /**//**==========================15个参数==================================================================================
         *@param:x0>圆心x坐标=========1
         *@param:y0>圆心y坐标=========2
         *@param:a>长轴===============3
         *@param:b>短轴================4
         *@param:h>厚度=================5
         *@alpha:Number>透明度===============6
         *@param:dataList>数据列表(有默认值)===============7
         *@param:dataList>颜色列表(有默认值)==================8
         *@param:dataList>名称列表(有默认值)==================9
         *@param:dataList>是否显示百分比==================10
         *@param:dataList>百分比字体颜色==================11
         *@param:dataList>是否显示图例==================12
         *@param:dataList>图例x坐标==================13
         *@param:dataList>图例y坐标==================14
         *@param:dataList>图例间距==================15
         */
         public function DrawPieGraph(x0:Number,y0:Number,a:Number,b:Number,h:Number,Alpha:Number,dataList:Array,colorList:Array,nameList:Array,ifpercent:Boolean,perColor:uint ,ifshoworder:Boolean, myorderx:Number, myordery:Number, myorderxy:Number) {
             _x0 = x0;
             _y0 = y0;
             _a = a;
             _b = b;
             _h = h;

			 trace(dataList,nameList)
             percent = ifpercent;
             fontcolor = perColor;
             showorder = ifshoworder;
             orderx = myorderx;
             ordery = myordery;
             orderxy = myorderxy;
             _nameList = nameList;
             _alpha = Alpha;
           _colorList = colorList == null ? [0x86A73E,0x913029,0x3F6491,0xD97D3C,0x331199,0x041215,
			 0x7F9B06,0x99F065,0xFE9F01,0xFE6600,0xFE929A,0xA0F101,0xFCD602,0x86A73E,
				 0x813029,0x3F6491,0xD77D3C,0x335199,0x046215]:colorList;
             sp=new Sprite();
             sp.mouseEnabled = sp.mouseChildren = false;
             render(dataList);
         }
         public function render(dataList:Array ):void {
             
             R = -90;
             setAngleList(dataList);
             clearAll();
             drawRect();
             drawPie();
         }
 
         private function setAngleList(dataList:Array):void {
             _dataList = dataList == null ? [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]:dataList;
             _angleList = [];
             var totalData:int;
            var len:uint = _dataList.length;
            for (var j:uint=0; j < len; j++) {
                totalData += _dataList[j];
            }
            if (totalData==0) {
                for (j=0; j < len; j++) {
                    _dataList[j]=1;
                    totalData=len;
                }
            }

            for (j=0; j < len; j++) {
                if (j==len-1) {
                    _angleList.push([R,270]);
                } else {
                   var r:uint=Math.floor(_dataList[j]/totalData*360);
                    var posR:int=R+r;
                   _angleList.push([R,posR]);
                    R=posR;
                }
            }
/*            for (j=len-1; j >0; j--) {
                
                if(_dataList[j]!=0)
				{
                    _angleList.splice(j, 1,[_angleList[j][0],270]);
                    break;
                }  
            }*/

        }
        private function setDepths():void {
            _depthList=[];
            var len:uint=_angleList.length;
            for (var j:uint=0; j < len; j++) {
                var minJ:Number=_angleList[j][0];
                var maxJ:Number=_angleList[j][1];
                switch (true) {
                    case minJ >= -90 && minJ <= 90 && maxJ<=90 :
                        _depthList[j]=minJ;
                        break;
                    default :
                        _depthList[j]=1000-minJ;
                }
            }
            _depthList=_depthList.sort(Array.NUMERIC|Array.RETURNINDEXEDARRAY);
            for (j=0; j<len; j++) {
                if (this.contains(__contain["shape"+_depthList[j]])) {
                    setChildIndex(__contain["shape"+_depthList[j]],j);
                }
            }
        }
		
		
        private function drawRect():void {
			var len:int = _angleList.length;	
			var sp:Sprite = new Sprite;
			sp.graphics.beginFill(0xffffff,0);
			sp.graphics.drawRect(300,320,180,50*len);
			this.addChild(sp);
            if (showorder) {
                for (var jk:uint=0; jk < len; jk++) {
                    Rect=new Sprite  ;//图例方块
                    sp.addChild(Rect);
                    Rect.name="abcde"+jk;
                    if (_dataList[jk]!=0) {
                        Rect.buttonMode=true;
                    }

                    namelabel=new TextField  ;//图例的文字说明，它和方块同为pie的子级
                    namelabel.text=_nameList[jk];
                    namelabel.selectable=false;
                    namelabel.autoSize=TextFieldAutoSize.LEFT;
                    namelabel.htmlText="<font size='24' color='#000000' face='微软雅体' >"+_nameList[jk]+"</font>";
                    //下划线效果namelabel.htmlText= "<font size='16' color='#ffffff' face='黑体' >"+"<u>"+_nameList[jk]+"</u>"+"</font>";
                    sp.addChild(namelabel);

                    var beginx:Number=orderx;
                    var beginy:Number=ordery;
                    var jianxy:Number=orderxy+12;//图例方块的边长
                    if (_dataList[jk]!=0) {
                        Rect.graphics.beginFill(_colorList[jk],1);
                    }
                    Rect.graphics.lineStyle(1,0xffffff,1);
                    Rect.graphics.moveTo(0,0);
                    Rect.graphics.lineTo(12,0);
                    Rect.graphics.lineTo(12,12);
                    Rect.graphics.lineTo(0,12);
                    Rect.graphics.lineTo(0,0);
                    if (_dataList[jk]!=0) {
                        Rect.graphics.endFill();
                    }
                    Rect.x=beginx;
                    Rect.y=beginy+jianxy*jk;
                    namelabel.x=Rect.x+20;
                    namelabel.y=Rect.y-4;
                    _dataList[jk]!=0?Rect.addEventListener(MouseEvent.MOUSE_OVER,onMouseDownX):0;
                }
				
				var pshl:PhScrollBar = new PhScrollBar(sp,180,300,true);
				pshl.x = 470;
				pshl.maskShape.x = 300;	
				pshl.maskShape.y = 320;
				pshl.startPoint = 10;
				sp.mask = pshl.maskShape;
				this.addChild(pshl);
				
            }


        }
        public function drawPie():void {
            __contain={};
            var len:uint=_angleList.length;
            var step:uint=1;

            shape=new Array();
            for (var j:uint=0; j < len; j++) {

                __contain["shape"+j]=new MovieClip  ;
                addChild(__contain["shape"+j]);
                shape.push(__contain["shape" + j]);
                if (_dataList[j]!=0) {

                    __contain["shape"+j].out=false;
                    //设置中心角，方便以下进行中点移动
                    __contain["shape"+j].r=(_angleList[j][0]+_angleList[j][1])/2;
                    __contain["shape"+j].addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownX);

                    __contain["shape"+j].name="shape"+j;
                    var drakColor:uint=getDarkColor(_colorList[j]);//深色
                    var g:Graphics=__contain["shape"+j].graphics;
                    //====================================================================================================先画百分比
                    if (percent) {
                        var total:Number=0;
                        for (var jh:uint=0; jh <_dataList.length; jh++) {
                            total+=_dataList[jh];
                        }
                        var prelabel:TextField=new TextField  ;
                        prelabel.text=String(Math.floor((_dataList[j]/total)*10000)/100)+"%";
                        prelabel.selectable=false;
                        prelabel.background=true;
                        prelabel.backgroundColor=drakColor;
                        __contain["shape"+j].addChild(prelabel);
                        var myformat:TextFormat = new TextFormat();
                        myformat.font="微软雅体";
                        myformat.size=12;
                        myformat.color=0xffffff;
                        prelabel.setTextFormat(myformat);
                        prelabel.gridFitType="pixel";
                        prelabel.antiAliasType="advanced";
                        prelabel.sharpness=-400;
                        prelabel.autoSize=TextFieldAutoSize.LEFT;
                        var zhongwei:Number=(_angleList[j][0] + _angleList[j][1] ) / 2;
                        if (zhongwei>0&&zhongwei<90) {
                            prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x;
                            prelabel.y=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).y;
                        } else if (zhongwei>90 && zhongwei<180) {
                            prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x-prelabel.width;
                            prelabel.y=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).y;
                        } else if (zhongwei>180 && zhongwei<270) {
                            prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x-prelabel.width;
                            prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height;
                        } else if (zhongwei>-90 && zhongwei<0) {
                            prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x;
                            prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height;
                        } else if (zhongwei == 0) {
                            prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x;
                            prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height/2;
                        } else if (zhongwei == 90) {
                            prelabel.x=getRPoint(_x0,_y0+_h,_a,_b,zhongwei).x-prelabel.width/2;
                            prelabel.y=getRPoint(_x0,_y0+_h+1,_a,_b,zhongwei).y;
                        } else if (zhongwei == 180) {
                            prelabel.x=getRPoint(_x0,_y0,_a,_b,zhongwei).x-prelabel.width;
                            prelabel.y=getRPoint(_x0,_y0,_a,_b,zhongwei).y-prelabel.height/2;
                        }
                    }
                    //-------------------------------------- //内弧
                    //g.lineStyle(1);
                    //先画底
                    //内弧
                    g.lineStyle(1,drakColor,_alpha);
                    g.beginFill(_colorList[j],_alpha);
                    g.moveTo(_x0,_y0+_h);
                    var r:Number=_angleList[j][0];
                    var minR:Number=r;
                    var maxR:Number=_angleList[j][1];
                    while (r + step < maxR) {
                        g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,r).x,getRPoint(_x0,_y0 + _h,_a,_b,r).y);
                        r+=step;
                    }
                    g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
                    g.endFill();
                    //画内侧面
                    g.lineStyle(1,drakColor,_alpha);
                    g.beginFill(drakColor,_alpha);
                    g.moveTo(_x0,_y0+_h);
                    g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
                    g.lineTo(getRPoint(_x0,_y0,_a,_b,minR).x,getRPoint(_x0,_y0,_a,_b,minR).y);
                    g.lineTo(_x0,_y0);
                    g.endFill();
                    //画外侧面
                    g.lineStyle(1,drakColor,1);
                    g.beginFill(drakColor,_alpha);
                    g.moveTo(_x0,_y0+_h);
                    g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,maxR).x,getRPoint(_x0,_y0 + _h,_a,_b,maxR).y);
                    g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
                    g.lineTo(_x0,_y0);
                    g.endFill();
                    //画外弧侧面
                    //画外弧侧面
                    //--------------------------------
                    //下边高
                    //下边外侧边高
                    if (minR<=0&&maxR>=0&&maxR<=180) {
                        //解决90度时外侧面，显示事实为90度，实际为0度，因为我们整个旋转了-90度；
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
                        var k:Number=minR;
                        while (k < 0) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,0).x,getRPoint(_x0,_y0,_a,_b,0).y);
                        while (k > minR) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,0).x,getRPoint(_x0,_y0 + _h,_a,_b,0).y);
                        k=0;
                        while (k < maxR) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
                        while (k > 0) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                    } else if (maxR >= 180 && minR <= 180 && minR >= 0) {
                        //解决180度时外侧面，显示事实为270度，实际为180度，因为我们整个旋转了-90度；
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
                        k=minR;
                        while (k < 180) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,180).x,getRPoint(_x0,_y0,_a,_b,180).y);
                        while (k > minR) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,180).x,getRPoint(_x0,_y0 + _h,_a,_b,180).y);
                        k=180;
                        while (k < maxR) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
                        while (k > 180) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                    } else if (minR <= 0 && maxR >= 180) {
                        //解决同时处于0度和180度的情况
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
                        k=minR;
                        while (k < 0) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,0).x,getRPoint(_x0,_y0,_a,_b,0).y);
                        while (k > minR) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,0).x,getRPoint(_x0,_y0 + _h,_a,_b,0).y);
                        k=0;
                        while (k < 180) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,180).x,getRPoint(_x0,_y0,_a,_b,180).y);
                        while (k > 0) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,180).x,getRPoint(_x0,_y0 + _h,_a,_b,180).y);
                        k=180;
                        while (k < maxR) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
                        while (k > 180) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                    } else {
                        g.lineStyle(1,drakColor,1);
                        g.beginFill(drakColor,1);
                        g.moveTo(getRPoint(_x0,_y0 + _h,_a,_b,minR).x,getRPoint(_x0,_y0 + _h,_a,_b,minR).y);
                        k=minR;
                        while (k < maxR) {
                            k+=step;
                            g.lineTo(getRPoint(_x0,_y0 + _h,_a,_b,k).x,getRPoint(_x0,_y0 + _h,_a,_b,k).y);
                        }
                        //g.lineTo(getRPoint(_x0, _y0+_h, _a, _b, maxR).x, getRPoint(_x0, _y0+_h, _a, _b, maxR).y);
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        while (k > minR) {
                            k-=step;
                            g.lineTo(getRPoint(_x0,_y0,_a,_b,k).x,getRPoint(_x0,_y0,_a,_b,k).y);
                        }
                        g.endFill();
                    }
                    //画上表面
                    g.lineStyle(1,_colorList[j],_alpha);
                    g.beginFill(_colorList[j],_alpha);
                    g.moveTo(_x0,_y0);
                    r=minR;
                    while (r + step < maxR) {
                        g.lineTo(getRPoint(_x0,_y0,_a,_b,r).x,getRPoint(_x0,_y0,_a,_b,r).y);
                        r+=step;
                    }
                    g.lineTo(getRPoint(_x0,_y0,_a,_b,maxR).x,getRPoint(_x0,_y0,_a,_b,maxR).y);
                    g.endFill();
                }
            }
            setDepths();
            addChild(sp);//提示标签加在这里，防止标签被饼块遮住
        }
        private function onMouseDownX(e:MouseEvent):void {
            var TG:MovieClip=shape[Number(e.currentTarget.name.substring(5))] as MovieClip;
            var posX:Number=getRPoint(0,0,D,D,TG.r).x;
            var posY:Number = getRPoint(0, 0, D, D, TG.r).y;

        }

        //===============================================================================
        private function getDarkColor(color:uint):uint {
            var r:uint=color>>16&0xFF/1.3;//设置red通道的值
            var g:uint=color>>8&0xFF/1.3;//设置green通道的值
            var b:uint=color&0xFF/1.1;//设置blue通道的值
            return r << 16 | g << 8 | b;//得到新颜色
        }
        private function getRPoint(x0:Number,y0:Number,a:Number,b:Number,r:Number):Object {
            r=r*Math.PI/180;
            return {x:Math.cos(r) * a + x0,y:Math.sin(r) * b + y0};
        }
        public function get contain():Object {
            return __contain;
        }
        private function clearAll():void {//清除内容
        
            var len:uint=_dataList.length;
            while(this.numChildren!=0){
                removeChildAt(0);
                }
            for (var j:uint=0; j<len; j++) {
                
                if (__contain) {
                    __contain["shape"+j]=null;
                }
            }
            
            Rect=null;

        }
    }
}